/**
 * Class:				BioInspiredCarCommander.java
 * 
 * Project:				Bio-inspired car commander
 * 
 * Subproject:			Word Detector
 * 
 * Date: 				Mai - Juillet 2011
 * 
 * Auteurs:				Bruno Da Silva
 * 						Thomas Jordan
 * 
 * Description:
 * Squelette de l'application. S'occupe de la mise en place des ressources utiles
 * pour l'application, charge les fichiers, et gère le menu principal de l'application.
 * 
 */
package ch.sda.bioinspiredcarcommander;

import java.io.File;
import java.util.LinkedList;

import mfccExtraction.FileManager;
import mfccExtraction.WordDecoder;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import ch.sda.bioinspiredcarcommander.words.WordsManager;
import edu.cmu.sphinx.frontend.window.RaisedCosineWindower;

public class BioInspiredCarCommander extends Activity {


	//File containing the last trained ann locally serialized into a file
	public static final String ANN_FILE = "/sdcard/SDA/ann.bin";
	public static final String CONFIG_FILE = "/sdcard/SDA/config.xml";
	//Filepath for the online generated training dataset
	public static final String dynamicLearningDatasetPath = "/sdcard/SDA/dataset.txt";

	//File that contains raw data for local training // Meme que wodDetectionActivity.java
	public static final String MFCC_FILE_L = "/sdcard/SDA/learningBase.txt";
	public static final String MFCC_FILE_P = "/sdcard/SDA/currentWord.txt";
	static WordDecoder wordDetector;

	static WordsManager wm;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		final Context ctx = this;


		//Load the wordList and the ann
		initializeComponents();

		ListView lv = (ListView) findViewById(R.id.listView1);

		String[] titles = new String[] {"Word Detection","Network Training","Words"};
		lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , titles));

		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent();
				Class<?> c = null;

				switch (position) {
				case 0:
				case 1:
					c = WordDetectionActivity.class;
					intent.putExtra("training", position==1);
					break;
				case 2:
					c = WordsActivity.class;
					break;
				default:
					break;
				}

				intent.setClass(ctx, c);

				ctx.startActivity(intent);
			}
		});		

	}

	private void initializeComponents()
	{

		new File(CONFIG_FILE).delete();
		writeConfigFile();

		wm = WordsManager.sharedManager(this);
		wm.reset();
		wm.addWord("play");
		wm.addWord("stop");
		wm.addWord("suivant");
		//wm.addWord("reculer");
		//wm.addWord("GPS");
		//wm.addWord("MP3");
		//wm.addWord("Appeler");
		wm.save();


		wordDetector = new WordDecoder(ANN_FILE,MFCC_FILE_L,MFCC_FILE_P , dynamicLearningDatasetPath, this, wm);		
		Log.v("LOL",wordDetector.getNbLearnSampleForWords().toString());

	}

	private void writeConfigFile()
	{
		LinkedList <String> file = new LinkedList<String>();

		file.add("<?xml version='1.0' encoding='UTF-8'?>");
		file.add("<config>");
		file.add("<property name='logLevel' value='WARNING'/>");
		file.add("<component name='mfcFrontEnd' type='edu.cmu.sphinx.frontend.FrontEnd'>");
		file.add("<propertylist name='pipeline'>");
		file.add("<item>streamDataSource</item>");
		file.add("<item>preemphasizer</item>");
		file.add("<item>windower</item>");
		file.add("<item>fft</item>");
		file.add("<item>melFilterBank</item>");
		file.add("<item>dct</item>");
		file.add("</propertylist>");
		file.add("</component>");
		file.add("<component name='streamDataSource' type='edu.cmu.sphinx.frontend.util.StreamDataSource'>");
		file.add("<property name='sampleRate' value='8000'/>");
		file.add("</component>");
		file.add("<component name='preemphasizer' type='edu.cmu.sphinx.frontend.filter.Preemphasizer'/>");
		file.add("<component name='windower' type='edu.cmu.sphinx.frontend.window.RaisedCosineWindower'>");
		file.add("<property name='" + RaisedCosineWindower.PROP_WINDOW_SHIFT_MS + "' value='500'/>");
		//file.add("<property name='" + RaisedCosineWindower.PROP_WINDOW_SIZE_MS + "' value='500'/>");
		file.add("</component>");
		file.add("<component name='fft' type='edu.cmu.sphinx.frontend.transform.DiscreteFourierTransform'>");
		file.add("<property name='numberFftPoints' value='4096'/>");
		file.add("</component>");
		file.add("<component name='melFilterBank' type='edu.cmu.sphinx.frontend.frequencywarp.MelFrequencyFilterBank'>");
		file.add("<property name='numberFilters' value='31'/>");
		file.add("<property name='minimumFrequency' value='200'/>");
		file.add("<property name='maximumFrequency' value='3500'/>");
		file.add("</component>");
		file.add("<component name='dct' type='edu.cmu.sphinx.frontend.transform.DiscreteCosineTransform'/>");
		file.add("</config>");

		FileManager.ecrireFichier(CONFIG_FILE, file);

	}

	public WordDecoder getWordDetector() { return wordDetector; }
}